package com.eagle.view.activity.adapter;

import android.content.Context;
import android.hardware.Camera;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eagle.EApplication;
import com.eagle.R;
import com.eagle.support.common.CameraDataCallback;
import com.eagle.view.widget.CameraTextureView;
import com.eagle.view.widget.ConferenceRemoteVideoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by gc on 2017/1/12.
 */
public class MeetingAdapter extends RecyclerView.Adapter {

    private static final int ITEM_TYPE_MINE = 0;

    private static final String DEFAULT_WINDOW = "00000000";

    public Context mContext;

    protected List<String> items = new ArrayList<>();

    private LayoutInflater mInflater;

    private CameraTextureView mCameraTextureView;

    private HashMap<String, ConferenceRemoteVideoView> mViewMaps = new HashMap<>();

    private double mCurWidth;

    private double mCurHeight;

    private ICameraDataCallback mListener;

    public MeetingAdapter(Context context, ICameraDataCallback lis) {
        this.mContext = context;
        this.mListener = lis;
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_MINE) {
            return new MeetingMineItemView(mInflater.inflate(R.layout.layout_meeting_local_item, parent, false));
        } else {
            return new MeetingOtherItemView(mInflater.inflate(R.layout.layout_meeting_remote_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MeetingMineItemView) {
            MeetingMineItemView itemView = (MeetingMineItemView) holder;
            mCameraTextureView = itemView.getCameraTextureView();
        } else if (holder instanceof MeetingOtherItemView) {
            MeetingOtherItemView itemView = (MeetingOtherItemView) holder;
            if (!mViewMaps.containsKey(items.get(position))) {
                mViewMaps.put(items.get(position), itemView.getConferenceRemoteVideoView());
            }
        }
    }

    public CameraTextureView getCameraTextureView() {
        return mCameraTextureView;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE_MINE;
        }
        return super.getItemViewType(position);
    }

    /**
     * 添加自己视角
     */
    public void initMineWindow() {
        if (!items.contains(DEFAULT_WINDOW)) {
            items.add(DEFAULT_WINDOW);
        }
        notifyDataSetChanged();
    }

    public void addRemoteWindow(String cid) {
        if (!items.contains(cid)) {
            items.add(cid);
            Log.e("cim", "addRemoteWindow: " + cid);
            notifyDataSetChanged();
        }
    }

    public void removeRemoteWindow(String cid) {
        if (items.contains(cid)) {
            Iterator iter = mViewMaps.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (entry.getKey().equals(cid)) {
                    ((ConferenceRemoteVideoView) entry.getValue()).doClose();
                    mViewMaps.remove(entry.getKey());
                }
            }
            items.remove(cid);
            notifyDataSetChanged();
        }
    }

    public HashMap<String, ConferenceRemoteVideoView> getViewMaps() {
        return mViewMaps;
    }

    public class MeetingMineItemView extends RecyclerView.ViewHolder implements CameraDataCallback {

        private CameraTextureView mCameraTextureView;

        public MeetingMineItemView(View itemView) {
            super(itemView);
            mCameraTextureView = (CameraTextureView) itemView.findViewById(R.id.layout_local);
            mCameraTextureView.setDataCallback(this);
            resetParams(mCameraTextureView);
            mCameraTextureView.setPreviewAreaSize((int) mCurWidth, (int) mCurHeight);
        }

        public CameraTextureView getCameraTextureView() {
            return mCameraTextureView;
        }

        @Override
        public void onCameraData(byte[] data, Camera camera) {
            if (mListener != null) mListener.onCameraData(data, camera);
        }
    }

    public class MeetingOtherItemView extends RecyclerView.ViewHolder {

        private ConferenceRemoteVideoView mConferenceRemoteVideoView;

        public MeetingOtherItemView(View itemView) {
            super(itemView);
            mConferenceRemoteVideoView = (ConferenceRemoteVideoView) itemView.findViewById(R.id.layout_remote);
            resetParams(mConferenceRemoteVideoView);
            mConferenceRemoteVideoView.setTag(items.get(getPosition()));
        }

        public ConferenceRemoteVideoView getConferenceRemoteVideoView() {
            return mConferenceRemoteVideoView;
        }
    }

    public void resetParams(View view) {
        mCurWidth = EApplication.getInstance().getScreenWidth() / 2;
        mCurHeight = (mCurWidth / 240) * 320;
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        params.width = (int) mCurWidth;
        params.height = (int) mCurHeight;
        view = new CameraTextureView(mContext, (int) mCurWidth, (int) mCurHeight);
        view.setLayoutParams(params);
    }

    public double getCurWidth() {
        return mCurWidth;
    }

    public double getCurHeight() {
        return mCurHeight;
    }

    public interface ICameraDataCallback {
        void onCameraData(byte[] data, Camera camera);
    }
}
